<?php

/**
 * 开放商户 - 结算账户变更功能使用示例
 */

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Openmerch\Change\ChangeService;
use JLPay\Models\Openmerch\Change\SettleAccountChangeRequest;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

// =================== 日志配置 =====================
$logger = Logger::getInstance();

// =================== 工具函数 =====================
function createConfig(): OrgConfig
{
    return DemoConfig::createConfigWithCrypto();
}

// =================== 数据构建函数 =====================
function buildSettleAccountChangeRequest(): SettleAccountChangeRequest
{
    $request = new SettleAccountChangeRequest();
    
    // 构建辅助证明材料
    $proofInfo = [
        [
            "proof_type" => "01",  // 证明类型：01-营业执照
            "proof_name" => "营业执照",
            "proof_url" => "https://example.com/license.jpg"
        ],
        [
            "proof_type" => "02",  // 证明类型：02-身份证
            "proof_name" => "法人身份证",
            "proof_url" => "https://example.com/idcard.jpg"
        ]
    ];
    
    // 设置请求参数
    $request->setApplyId("b8659ea518d14c99b78b5ad000a0803b")  // 使用新的申请单号
            ->setMerchNo("849584358140109")  // 使用新的商户号
            ->setSettleType("01")  // 结算类型：01-对私
            ->setAccountName("张三")
            ->setAccountNo("6222021234567890123")
            ->setBankCode("ICBC")  // 工商银行
            ->setBankBranchId("102100000000")
            ->setBankBranchName("工商银行北京朝阳支行")
            ->setAccountPic("https://example.com/account.jpg")
            ->setBankSetPhone("13800138000")
            ->setCertType("01")  // 证件类型：01-身份证
            ->setDelegateeIdCard("110101199001011234")
            ->setDelegateeCertPeriodBegin("2020-01-01")
            ->setDelegateeCertPeriodEnd("2030-01-01")
            ->setAcctLicense("https://example.com/license.jpg")
            ->setAcctCertFrontPic("https://example.com/idcard_front.jpg")
            ->setAcctCertBackPic("https://example.com/idcard_back.jpg")
            ->setAcctAuthLetter("https://example.com/auth_letter.jpg")
            ->setIdentityFacePic("https://example.com/face.jpg")
            ->setAcctFacePic("https://example.com/acct_face.jpg")
            ->setProofInfo($proofInfo);
    
    return $request;
}

// =================== 示例函数 =====================
function settleAccountChangeDemo(): void
{
    global $logger;

    $logger->info("开始执行结算账户变更示例");
    
    // 创建配置对象和变更服务
    $config = createConfig();
    $changeService = new ChangeService($config);
    
    // 创建结算账户变更请求
    $request = buildSettleAccountChangeRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $changeService, 
        $request, 
        "结算账户变更",
        function($service, $request) {
            return $service->changeSettleAccount($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("结算账户变更示例执行完毕");
}

// =================== 主程序 =====================
$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 结算账户变更示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

settleAccountChangeDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 结算账户变更示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 